import Vue from 'vue'
import Vuex from 'vuex'
import api from "@/api/api"
import MinCache from'@/common/util/MinCache.js'
import { ACCESS_TOKEN,USER_NAME,USER_INFO,X_TENANT_ID } from "@/common/util/constants"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: uni.getStorageSync(ACCESS_TOKEN) ? uni.getStorageSync(ACCESS_TOKEN) : '',
    userid:'',
    username: uni.getStorageSync(USER_INFO) ? uni.getStorageSync(USER_INFO).username : '',
    realname: uni.getStorageSync(USER_INFO) ? uni.getStorageSync(USER_INFO).realname : '',
    avatar: '',
	merchant: uni.getStorageSync('merchant') ? JSON.parse(uni.getStorageSync('merchant')) : '',
	scene: uni.getStorageSync('scene') ? uni.getStorageSync('scene') : '',
	dicts: uni.getStorageSync('dicts') ? JSON.parse(uni.getStorageSync('dicts')) : [],
  },
  mutations: {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, { username, realname }) => {
        state.username = username
        state.realname = realname
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
	SET_MERCHANT(state, param) {
		state.merchant = param.merchant;
		uni.setStorageSync('merchant', JSON.stringify(param.merchant))
	},
	SET_DICTS(state, param) {
		state.dicts = param.dicts;
		uni.setStorageSync('dicts', JSON.stringify(param.dicts))
	},
	SET_SCENE(state, param) {
		state.scene = param.scene;
		uni.setStorageSync('scene', JSON.stringify(param.scene))
	}
  },
  actions: {
    // 登录
    mLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
       api.login(userInfo).then(response => {
          if(response.data.code ==200){ 
            const result = response.data.result
            const userInfo = result.userInfo
			uni.setStorageSync(ACCESS_TOKEN,result.token);
			uni.setStorageSync(USER_INFO,userInfo);
            commit('SET_TOKEN', result.token)
            commit('SET_AVATAR', userInfo.avatar)
            commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
			commit('SET_DICTS', {dicts: result.sysAllDictItems})
            resolve(response)
          }else{
            resolve(response)
          }
        }).catch(error => {
			console.log("catch===>response",response)
          reject(error)
        })
      })
    },
    //手机号登录
    PhoneLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.phoneNoLogin(userInfo).then(response => {
          if(response.data.code ==200){
            const result = response.data.result
            const userInfo = result.userInfo
			uni.setStorageSync(ACCESS_TOKEN,result.token);
			uni.setStorageSync(USER_INFO,userInfo);
            commit('SET_TOKEN', result.token)
            commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
            commit('SET_AVATAR', userInfo.avatar)
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 第三方登录
    ThirdLogin({ commit }, param) {
      return new Promise((resolve, reject) => {
        api.thirdLogin(param.token,param.thirdType,param.tenantId).then(response => {
          if(response.data.code =='200'){
            const result = response.data.result
            const userInfo = result.userInfo
            uni.setStorageSync(ACCESS_TOKEN,result.token);
            uni.setStorageSync(USER_INFO,userInfo);
            uni.setStorageSync(X_TENANT_ID,userInfo.loginTenantId);

            commit('SET_TOKEN', result.token)
            commit('SET_AVATAR', userInfo.avatar)
            commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname })
            commit('SET_ID', userInfo.id)
            commit('SET_INFO', userInfo)
            commit('SET_TENANTID', userInfo.loginTenantId)
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
        commit('SET_TOKEN', '')
		commit('SET_MERCHANT', {merchant: ''})
        uni.removeStorageSync(ACCESS_TOKEN)
        api.logout(logoutToken).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        })
      })
    },
	setMerchant: ({
		commit,
		state
	}, param) => {
		commit('SET_MERCHANT', param);
	},
	setDicts: ({
		commit,
		state
	}, param) => {
		commit('SET_DICTS', param);
	},
	setScene: ({
		commit,
		state
	}, param) => {
		commit('SET_SCENE', param);
	}
  },
  getters:{
    
  }
})
